package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.collect.community.R;
import com.csoa.community.adapter.WZMLTableAdapter;
import com.csoa.community.bean.WZMLInfoBean;
import com.csoa.community.db.WZMLInfoDB;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.PullToRefreshBase.OnRefreshListener;
import com.csoa.community.widget.PullToRefreshListView;

public class WZMLInfoTableActivity extends BaseActivity {

	private ListView mList;
	private EditText etWZMLLadinTable;
	private EditText etWZMLChineseTable;
	public static List<WZMLInfoBean> mWZMLInfos;
	private WZMLTableAdapter adapter;
	private EditText etWZMLAreaTable;
	private PullToRefreshListView mPullToRefreshListView;
	public int mCurrentPage = 1;


	@Override
	public void initUI() {
		
		setContentView(R.layout.wzml_info_table);
		mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
		mList = mPullToRefreshListView.getRefreshableView();
		etWZMLLadinTable = (EditText)this.findViewById(R.id.etWZMLLadinTable);
		etWZMLChineseTable = (EditText)this.findViewById(R.id.etWZMLChineseTable);
		etWZMLAreaTable = (EditText)this.findViewById(R.id.etWZMLAreaTable);
		mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				if(mPullToRefreshListView.hasPullFromTop()) {
					refresh();
				}else {
					mCurrentPage++;
					List<WZMLInfoBean> wzmlInfos = WZMLInfoDB.query(mCurrentPage);
					if(wzmlInfos != null && wzmlInfos.size() != 0) {
						mWZMLInfos.addAll(wzmlInfos);
						adapter.notifyDataSetChanged();
					}else {
						_activity.showToast("没有更多物种目录信息了");
						mCurrentPage -- ;
					}
				}
				
			}
		});
		
		this.findViewById(R.id.btnRefreshWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						refresh();
					}
				});
		this.findViewById(R.id.btnBackWZMLTable).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
		
		this.findViewById(R.id.btnQueryWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						String latinName = etWZMLLadinTable.getText().toString().trim();
						String chineseName = etWZMLChineseTable.getText().toString().trim();
						String area = etWZMLAreaTable.getText().toString().trim();
						if(StringUtils.isEmpty(latinName) && StringUtils.isEmpty(chineseName) && StringUtils.isEmpty(area)) {
							WZMLInfoTableActivity.this.showToast("请输入任一搜索条件");
							return;
						}
						mWZMLInfos.clear();
						List<WZMLInfoBean> wzmlInfos = WZMLInfoDB.query(latinName,chineseName,area);
						if (wzmlInfos == null || wzmlInfos.size() == 0) {
							WZMLInfoTableActivity.this.showToast("您搜索的物种名录信息不存在");
						}else {
							mWZMLInfos.addAll(wzmlInfos);
						}
						adapter.notifyDataSetChanged();
					}
				});
		
		this.findViewById(R.id.btnAddWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(WZMLInfoTableActivity.this, WZMLInfoActivity.class);
						intent.putExtra("which", 1);
						startActivityForResult(intent, 0);
					}
				});
		
	}
	

	@Override
	public void initData() {
		mWZMLInfos = new ArrayList<WZMLInfoBean>();
		List<WZMLInfoBean> wzmlInfos = WZMLInfoDB.query(mCurrentPage);
		if (wzmlInfos != null) {
			mWZMLInfos.addAll(wzmlInfos);
		}
		adapter = new WZMLTableAdapter(this, mWZMLInfos);
		mList.setAdapter(adapter);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (resultCode == RESULT_OK) {
			if(intent != null) {
				WZMLInfoBean delWZMLInfo = (WZMLInfoBean)intent.getSerializableExtra("delete");
				if(delWZMLInfo != null) {//删除
					int position = intent.getIntExtra("position", 0);
					mWZMLInfos.remove(position);
				}else {
					WZMLInfoBean updateWZInfo = (WZMLInfoBean)intent.getSerializableExtra("update");
					if(updateWZInfo != null) {
						int position = intent.getIntExtra("position", 0);
						if(updateWZInfo != null) {//更新
							mWZMLInfos.remove(position);
							mWZMLInfos.add(position, updateWZInfo);
						}
					}else {//添加
						refresh();
						return;
					}
				}
				adapter.notifyDataSetChanged();
			}
		}
	}
	
	
	public void refresh() {
		mCurrentPage = 1;
		List<WZMLInfoBean> wzmlInfos = WZMLInfoDB.query(mCurrentPage);
		if(wzmlInfos != null) {
			mWZMLInfos.clear();
			mWZMLInfos.addAll(wzmlInfos);
			clearEdit();
			adapter.notifyDataSetChanged();
		}
	}
	
	public void clearEdit() {
		etWZMLAreaTable.setText("");
		etWZMLChineseTable.setText("");
		etWZMLLadinTable.setText("");
	}

}
